<template>
	<view class="content">
		<view class="top-block">
			<view class="iconfont icon-tishi"><text class="txt">请保证本人对信息内容的真实性和完整性。</text></view>
		</view>
		<view class="info-block">
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">姓名</view>
				</view>
				<input placeholder="请输入姓名" v-model="form.name" />
			</view>
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">手机号码</view>
				</view>
				<input placeholder="请输入手机号码" v-model="form.phoneNumber" type="number" maxlength="11" />
			</view>
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">身份证号码</view>
				</view>
				<input placeholder="请输入身份证号码" v-model="form.idCard" type="idcard" maxlength="18" />
			</view>
		</view>
		<view class="btn-item" @click="application">
			<view class="btn">立即报名</view>
		</view>
		<SuccessDialog v-if="showDialog" cancelText="返回首页" @callback="dialogCallback">
			<slot style="display: flex;flex-direction: column;">
				<image src="/static/images/shopping/illustration.png"
					style="width: 100%;height: 400rpx;margin-bottom: 50rpx;">
				</image>
				<view style="color: #157DFA;font-size: 38rpx;margin-bottom: 30rpx;"
					class="FangZhengHanZhenGuangBiaoJianTi">提交成功</view>
				<view style="color: #000;line-height: 54rpx; font-size: 30rpx;">
					您的报名提交成功，管理员会在1-3个工作日内审核，请耐心等待。</view>
			</slot>
		</SuccessDialog>
	</view>
</template>

<script>
	import {
		applicationApi
	} from '@/api/activity.js'
	import {
		updateUserInfoApi,
		getInfoApi,
		getselectTreeApi
	} from '@/api/mine.js'
	import {
		validate
	} from '@/util/validate.js'
	import {
		idCardValidate
	} from '@/util/idCardValidate';
	import SuccessDialog from '@/components/SuccessDialog/SuccessDialog.vue';
	import MultipleColPicker from '@/components/Picker/MultipleColPicker'
	import {
		getStorage,
		setStorage
	} from '@/util/auth.js';
	export default {
		components: {
			SuccessDialog,
			MultipleColPicker
		},
		data() {
			return {
				showDialog: false,
				auditStatus: 0,
				form: {
					name: '',
					phoneNumber: '',
					idCard: '',
				},
				rules: {
					name: {
						name: '姓名',
						required: true,
						validator: function(value, form, callback) {
							if (value.length < 2) {
								callback('请填写正确的姓名')
							}
							callback();
						}
					},
					phoneNumber: {
						name: '手机号码',
						required: true,
						validator: function(value, form, callback) {
							const phoneReg =
								/^((\+|00)86)?((134\d{4})|((13[0-3|5-9]|14[1|5-9]|15[0-9]|16[2|5|6|7]|17[0-8]|18[0-9]|19[0-2|5-9])\d{8}))$/;

							if (value.length != 11) {
								callback('请输入正确的手机号');
							} else if (!phoneReg.test(value)) {
								callback('请输入正确的手机号');
							}
							callback();
						}
					},
					idCard: {
						name: '身份证号',
						required: true
					}
				},
				type: null,
				id: 0,
				state: null
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			dialogCallback(e) {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			application() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (form.auditStatus == 2) {
					if (validate(form, this.rules)) {
						const idcard = idCardValidate(this.form.idCard);
						if (!idcard.result) {
							this.showToast('请输入正确身份证号码', () => {});
							return;
						}
						uni.showLoading({
							title: '提交中',
							mask: true
						});
						applicationApi({
							name: this.form.name,
							phoneNumber: this.form.phoneNumber,
							idCard: this.form.idCard,
							activityId: this.id,
							villageId: this.form.villageId
						}).then(res => {
							uni.hideLoading()
							this.showDialog = true
						}).catch(() => {
							uni.hideLoading()
						})
					}
				} else {
					if (this.form.auditStatus == 1) {
						uni.showModal({
							title: '提示',
							content: '请进行实名认证！',
							cancelText: '先逛逛',
							confirmText: '去认证',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/page_other/personal/index?type=1&&name=编辑个人资料&&auditStatus=1'
									})
								} else {
									uni.navigateBack();
								}
							}
						})
					} else {
						this.showToast('您的资料还未审核通过！')
					}
				}
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getInfoApi({}).then(res => {
					uni.hideLoading()
					this.form = Object.assign(this.form, res)
				}).catch(() => {
					uni.hideLoading()
				})
			}
		},
		onLoad(e) {
			this.id = e.id
			this.type = e.type
			uni.setNavigationBarTitle({
				title: e.name
			})
			this.getDetail()

		}
	}
</script>

<style lang="scss">
	.input-value-border {
		width: 430rpx;
		border: none !important;
		display: flex !important;
		justify-content: space-between !important;
	}

	.placeholder {
		color: #000 !important;
		font-size: 30rpx !important;
	}

	.item {
		padding: 0 30rpx !important;
		height: 80rpx !important;
	}

	.content {
		width: 100%;
		padding: 30rpx;

		.top-block {
			background: #CADFFA;
			padding: 30rpx;
			border-radius: 20rpx;
			align-items: center;

			.iconfont {
				color: #127BEC;
				font-size: 44rpx;
				display: flex;
				align-items: center;

				.txt {
					font-size: 30rpx;
					margin-left: 10rpx;
				}
			}
		}

		.info-block {
			width: 100%;
			margin-top: 30rpx;
			background: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;

			.info-item {
				display: flex;
				align-items: center;
				line-height: 120rpx;
				border-bottom: 1px solid #eee;
				width: 100%;

				.title-item {
					display: flex;
					align-items: center;
					width: 220rpx;

					.must {
						color: #ED3F3D;
					}
				}

				.form-content {
					width: calc(100 - 220rpx);

					.form-ctrl {
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}

			}

			.info-item:last-child {
				border-bottom: none;
			}
		}

		.btn-item {
			width: calc(100% - 60rpx);
			display: flex;
			justify-content: center;
			margin-top: 60rpx;

			.btn {
				background: #157DFA;
				color: #fff;
				width: 340rpx;
				height: 80rpx;
				border-radius: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>